package geektime.concurrent.race;
/**
 *
 **/

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * @Author weiwei
 * @Date 2021-12-31 10:31
 * @description
 **/
public class MyGen implements GenRunnable {

    MyShareData ssd;
    int size;
    int offset;

    public MyGen(MyShareData ssd, int size, int offset) {
        this.ssd = ssd;
        this.size = size;
        this.offset = offset;
    }

    @Override
    public void gen() {
        Random rand = new Random(System.currentTimeMillis()+offset);
        int r;
        int i;
        List<Integer> score = new LinkedList<>();
        for (i = 0; i < size; i++) {
            r = rand.nextInt(SimpleShareData.COUNT);
            score.add(r);
        }
        synchronized (ssd.getScore()){
            ssd.getScore().addAll(score);
        }
        //System.out.println("产生随机数个数: " + i);
        ssd.getGenSig().countDown();
    }

    @Override
    public void run() {
        gen();
    }
}
